import { Subscription } from 'egg';
import * as moment from 'moment';

export default class AlarmSubscription extends Subscription {

    // 通过 schedule 属性来设置定时任务的执行间隔等配置
    static get schedule() {
        return {
            cron: '0 */5 * * * *',
            type: 'worker',
        };
    }
    public async subscribe() {
        this.app.logger.info('alarm');
        const [todaySchedules, oneHourSchedules, tomorrowSchedules]
            = await Promise.all([this.service.db.orderedSchedule.get30Minites(),
            this.service.db.orderedSchedule.get1Hour(),
            this.service.db.orderedSchedule.getTomorrow()]);
        for (const schedule of todaySchedules) {
            this.app.logger.info('alarm: ' + schedule.id);
            const {
                teacher_phone,
                student_phone,
                teacher_name,
                student_name,
                date,
                time_from,
            } = schedule;
            const dateFormat = moment(date).format('YYYY年MM月DD日');
            const timeFromFormat = moment(time_from, 'HH:mm:SS').format('HH:mm');
            this.app.logger.info('half our Student-Phone: ' + student_phone);
            this.app.logger.info('half our Teacher-Phone: ' + teacher_phone);
            await this.service.sms.sendAlarmStudentToday(student_phone,
                student_name, teacher_name, `${dateFormat} ${timeFromFormat}`);
            await this.service.sms.sendAlarmTeacherToday(teacher_phone,
                student_name, teacher_name, `${dateFormat} ${timeFromFormat}`);
        }
        for (const schedule of tomorrowSchedules) {
            this.app.logger.info('alarm: ' + schedule.id);
            const {
                teacher_phone,
                student_phone,
                teacher_name,
                student_name,
                time_from,
                date,
            } = schedule;
            const dateFormat = moment(date).format('YYYY年MM月DD日');
            const timeFromFormat = moment(time_from, 'HH:mm:SS').format('HH:mm');
            this.app.logger.info('One day Student-Phone: ' + student_phone);
            this.app.logger.info('One day Teacher-Phone: ' + teacher_phone);
            await this.service.sms.sendAlarmStudentTomorrow(student_phone,
                student_name, teacher_name, `${dateFormat} ${timeFromFormat}`);
            await this.service.sms.sendAlarmTeacherTomorrow(teacher_phone,
                student_name, teacher_name, `${dateFormat} ${timeFromFormat}`);
        }
        for (const schedule of oneHourSchedules) {
            this.app.logger.info('alarm: ' + schedule.id);
            const {
                teacher_name,
                student_name,
                time_from,
                date,
                admin_id,
            } = schedule;
            const dateFormat = moment(date).format('YYYY年MM月DD日');
            const timeFromFormat = moment(time_from, 'HH:mm:SS').format('HH:mm');
            await this.service.db.msg.create(admin_id,
                `您好，今天${dateFormat} ${timeFromFormat}, ${teacher_name}有${student_name}的线上课`);
        }
    }
}
